package com.dm0858.bianmin.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.amap.api.location.CoordinateConverter;
import com.amap.api.location.DPoint;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.dm0858.bianmin.R;
import com.dm0858.bianmin.model.entity.NormalListData;
import com.dm0858.bianmin.utils.CacheUtils;
import com.dm0858.bianmin.utils.GlideUtils;

import java.io.File;
import java.net.URISyntaxException;
import java.util.List;

import okhttp3.Cache;

import static com.dm0858.bianmin.app.ApiConstant.BASE_SERVER_URL;
import static java.lang.Math.atan2;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static java.lang.Math.sqrt;


/**
 * Created by yelvqing on 2017/11/21.
 */

public class NormalListAdapter extends BaseQuickAdapter<NormalListData, BaseViewHolder> {
    /**
     * 普通的列表布局
     */
    private static final int NORMAL_TEXT_NEWS = 100;
    /**
     * 租房买房
     */

    private static final int ZUFANG_TEXT_NEWS = 200;

    /**
     * 二手汽车
     */
    private static final int ERSHOUCHE_TEXT_NEWS = 300;

    private Context mContext;
    public NormalListAdapter(Context context,  List<NormalListData> data) {
        super(data);
        mContext = context;


        //Step.1
        setMultiTypeDelegate(new MultiTypeDelegate<NormalListData>() {
            @Override
            protected int getItemType(NormalListData news) {

                if (news.mtype.equals("zufang")) {
                    return ZUFANG_TEXT_NEWS;
                }
                else if(news.mtype.equals("ershouche"))
                {
                    return ERSHOUCHE_TEXT_NEWS;
                }
                else
                {
                    return NORMAL_TEXT_NEWS;
                }

            }


        });
        //Step .2
        getMultiTypeDelegate()
                .registerItemType(NORMAL_TEXT_NEWS, R.layout.item_normal_list)//普通的列表布局
                .registerItemType(ZUFANG_TEXT_NEWS, R.layout.item_zufang_list)//租房买房布局
                .registerItemType(ERSHOUCHE_TEXT_NEWS, R.layout.item_ershouche_list);//二手汽车布局

    }


    @Override
    protected void convert(BaseViewHolder helper, NormalListData news) {
        if (TextUtils.isEmpty(news.title)){
            //如果没有标题，则直接跳过
            return;
        }


        //根据类型为不同布局的条目设置数据
        switch (news.mtype) {
            case "zufang":
            {

                GlideUtils.load(mContext, news.image, helper.getView(R.id.normal_img));

                helper.setText(R.id.normal_title, news.title);
                helper.setText(R.id.fangxing, news.huxing+" | ");
                helper.setText(R.id.mianji, news.mianji+" | ");
                helper.setText(R.id.xiaoqu, news.xiaoqu);
                helper.setText(R.id.zufang_tel, news.tel);
                if(news.addr!=null)
                {
                    helper.setText(R.id.zufang_addrtxt, news.addr);
                    helper.setVisible(R.id.zufang_addrimg, true);
                    helper.setVisible(R.id.zufang_addrtxt, true);
                }
                else
                {

                    helper.setVisible(R.id.zufang_addrtxt, false);
                    helper.setVisible(R.id.zufang_addrimg, false);
                }

                helper.setText(R.id.jiage, news.jiage);
            }
            break;
            case "ershouche":
            {

                GlideUtils.load(mContext, news.image, helper.getView(R.id.normal_img));

                helper.setText(R.id.normal_title, news.title);
                if(news.shangpai!=null)
                {
                    helper.setText(R.id.shangpai, news.shangpai);
                    helper.setVisible(R.id.shangpai, true);
                }
                else
                {

                    helper.setVisible(R.id.shangpai, false);
                }
                if(news.licheng!=null)
                {
                    helper.setText(R.id.licheng, news.licheng);
                    helper.setVisible(R.id.licheng, true);
                }
                else
                {

                    helper.setVisible(R.id.licheng, false);
                }

                helper.setText(R.id.ershouche_tel, news.tel);


                helper.setText(R.id.ershouche_jiage, news.jiage);
            }
                break;
            default:
            {
                GlideUtils.load(mContext, news.image, helper.getView(R.id.normal_img));


                // Gson gson = new Gson();
                //  Type type = new TypeToken<List<ListData>>() {
                //  }.getType();
                //  List<ListData> contacts = gson.fromJson(news.expand, type);

                //   KLog.e("cc="+contacts.get(0).value);
                helper.setText(R.id.normal_title, news.title);
                helper.setText(R.id.normal_addr, news.addr);
                helper.setText(R.id.normal_tel, news.tel);


 /*
        String[] beian_arr = news.beian.split(" ");
        View layout = new View(mContext);

        LinearLayout normal_layout = (LinearLayout)layout.findViewById(R.id.normal_beian);

        KLog.e("dd="+beian_arr.length);

        for (int i = 0 ; i <beian_arr.length ; i++ ) {

            BorderTextView textView = new BorderTextView(mContext);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            textView.setLayoutParams(layoutParams);
            textView.setText(beian_arr[i]);
            KLog.e("ee="+beian_arr[i]);
            textView.setTop(10);
            textView.setRight(10);
            textView.setTextSize(16);
            textView.setTextColor(Color.rgb(0, 0, 0));
            normal_layout.addView(textView);

        }*/
                if(news.beian!=null)
                    helper.setText(R.id.normal_beian, news.beian);
                else
                    helper.setVisible(R.id.normal_beian, false);

                helper.setText(R.id.normal_juli, news.juli);
                helper.getView(R.id.normal_juli).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //跳转

                            //  com.baidu.BaiduMap这是高德地图的包名
                            //调起百度地图客户端try {
                            Intent intent = new Intent();

                            intent.setData(Uri.parse("baidumap://map/marker?location="
                                    + news.lat + "," + news.lng + "&title="+ news.addr +"&content=&traffic=on&src=andr.baidu.openAPIdemo"));


                            if(isInstallByread("com.baidu.BaiduMap")){
                                mContext.startActivity(intent); //启动调用
                                Log.e("GasStation", "百度地图客户端已经安装") ;

                            }else{

                                if(isInstallByread("com.autonavi.minimap")){
                                    Log.e("GasStation", "高德地图客户端已经安装") ;
                                    goToGaode(news.lat,news.lng);
                                }else{
                                    Toast.makeText(mContext, "没有安装地图客户端", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }

                });



                helper.setVisible(R.id.shuxian, true);//
                helper.setVisible(R.id.callbtn, true);//

                helper.getView(R.id.callbtn).setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        //Toast.makeText(mContext, "你点击了按钮",Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+news.tel));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.getApplicationContext().startActivity(intent);
                    }
                });
                if (news.tel.equals("")) {
                    helper.getView(R.id.callbtn).setVisibility(View.GONE);
                }else {
                    helper.getView(R.id.callbtn).setVisibility(View.VISIBLE);

                }

            }

            break;
        }


    }


    //高德
    private void goToGaode(String lat, String lon) {

        CoordinateConverter converter  = new CoordinateConverter(mContext);
// CoordType.GPS 待转换坐标类型
        converter.from(CoordinateConverter.CoordType.GPS);
// sourceLatLng待转换坐标点 DPoint类型
        try {
            converter.coord(new DPoint(Double.parseDouble(lat),Double.parseDouble(lon)));
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(mContext, "解析经纬度出错", Toast.LENGTH_SHORT).show();

            return;
        }
// 执行转换操作
        DPoint desLatLng;

        try {
            desLatLng = converter.convert();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(mContext, "解析经纬度出错", Toast.LENGTH_SHORT).show();
            return;
        }

        StringBuffer stringBuffer = new StringBuffer("androidamap://route?sourceApplication=").append("amap");

        stringBuffer.append("&dlat=").append(desLatLng.getLatitude())
                .append("&dlon=").append(desLatLng.getLongitude())
                .append("&dname=").append("")
                .append("&dev=").append(0)
                .append("&t=").append(0);

        Intent intent = new Intent("android.intent.action.VIEW", android.net.Uri.parse(stringBuffer.toString()));
        intent.setPackage("com.autonavi.minimap");
        mContext.startActivity(intent);
    }





    /**
     * 判断是否安装目标应用
     * @param packageName 目标应用安装后的包名
     * @return 是否已安装目标应用
     */
    private boolean isInstallByread(String packageName) {
        return new File("/data/data/" + packageName).exists();
    }

}
